﻿using Lucene.Net.Search;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace FortuneArterial.Site.Laboratory.Controllers
{
    [DisplayName("盘古分词")]
    public class PanGuSegmentController
        : BaseController
    {
        public PanGuSegmentController()
        {
            _segment = new PanGu.Segment();
        }

        [DisplayName("简介")]
        public ActionResult Index()
        {
            return SmartView();
        }

        #region 功能对比
        [DisplayName("功能对比")]
        public ActionResult FeatureComparison()
        {
            return SmartView();
        }

        [HttpPost]
        public ActionResult SimpleSegment(string text)
        {
            ICollection<PanGu.WordInfo> words = _segment.DoSegment(text);

            StringBuilder sb = new StringBuilder();
            foreach (var wi in words)
            {
                sb.AppendFormat("{0}({1},{2})<br />", wi.Word, wi.Position, wi.Rank);
            }

            return Content(sb.ToString());
        }
        #endregion

        #region 比较拆分结果
        [DisplayName("比较拆分结果")]
        public ActionResult CompareTwoText()
        {
            return SmartView();
        }

        [HttpPost]
        public ActionResult CompareText(string text1, string text2)
        {
            double same = 0.0;
            ICollection<PanGu.WordInfo> words1 = _segment.DoSegment(text1);
            ICollection<PanGu.WordInfo> words2 = _segment.DoSegment(text2);

            foreach (var wi1 in words1)
            {
                foreach (var wi2 in words2)
                {
                    if (wi1.CompareTo(wi2) == 0)
                    {
                        same += 2;
                    }
                }
            }

            return Content((same / (words1.Count + words2.Count)).ToString("#,##0.00%"));
        }
        #endregion

        #region Lucene.Net
        [DisplayName("Lucene.Net")]
        public ActionResult LuceneDotNet()
        {
            return SmartView();
        }

        [HttpPost]
        public ActionResult LuceneDotNet(string q)
        {
            return SmartView();
        }
        #endregion

        //初始化盘古分词
        static PanGuSegmentController()
        {
            PanGu.Segment.Init();
        }

        private readonly PanGu.Segment _segment;
    }
}
